package org.cashbook.utilities.logger;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.cashbook.configuration.Constants;

public class LoggerTXT extends AbstractLogger {

	public LoggerTXT(String loggingLevel) {
		super(loggingLevel);
	}

	@Override
	protected void writeMessage(String message) {
		String zeile = null;
		ArrayList<String> log = new ArrayList<String>();
		
		try {
			FileReader fileIn = new FileReader(Constants.loggerFilePath);
			BufferedReader input = new BufferedReader(fileIn);
			while ((zeile = input.readLine()) != null) {
				log.add(zeile);
			}
			input.close();
		} catch (IOException e) {
			log.add("[MANUAL MESSAGE] File for saving logs not found, no logs have been loaded");
		}
		log.add(message);
		try {
			FileWriter fileOut = new FileWriter(Constants.loggerFilePath);
			BufferedWriter output = new BufferedWriter(fileOut);
			for(int i = 0; i < log.size(); i++) {
				output.write(log.get(i));
				output.newLine();
			}
			output.close();			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
